/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Dec 20, 2011  2:31:09 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : sap.jco.server
 * File Name : ServerProvider.java
 * TODO
 */
package sap.jco.server;

import org.apache.log4j.Logger;

import sap.business.fi.inbound.PaymentRequestOutgoingService;
import sap.business.fi.inbound.ReReceiptedService;
import sap.business.mm.inbound.MmConvFactInbService;
import sap.business.mm.inbound.MmMatmasInbService;
import sap.business.mm.inbound.MmPlantInbService;
import sap.business.mm.inbound.MmPurchaseOrderService;
import sap.business.mm.inbound.MmSuppdebitnotesinbService;
import sap.business.mm.inbound.MmVendorMasInbService;
import com.sap.mw.jco.IRepository;
import com.sap.mw.jco.JCO;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Dec 4, 2013 : 10:18:11 AM 
 * CreateBy=Chatree Siangklom
 * ServerProvider
 * TODO
 */
public class ServerProvider extends JCO.Server {
	
	private static final Logger log = Logger.getLogger(ServerProvider.class);
	
	//public final Connection conn_normal;
	
	/**
     *  Create an instance of my own server
     *  @param gwhost (gateway host)
     *  @param gwserv (gateway service number)
     *  @param progid (program id)
     *  @param isUnicode (is unicode)
     *  @param repository (repository used by the server to lookup the 
       definitions of an inc)
     */
    public ServerProvider(String gwhost, String gwserv, String progid,boolean isUnicode, IRepository repository) {
    	super(gwhost,gwserv,progid,repository);    	    	
    	this.setProperty("jco.server.unicode", isUnicode?"1":"0");
    	//this.conn_normal = new dbHelper().getConn();
    }
    
    protected void handleRequest(JCO.Function function) {
    	
        JCO.ParameterList input  = function.getImportParameterList();
        JCO.ParameterList output = function.getExportParameterList();
        JCO.ParameterList tables = function.getTableParameterList();
        
        //System.out.println("handleRequest(" + function.getName() + ")");
        log.info("handleRequest(" + function.getName() + ")");        
        
        if(function.getName().equals("ZCREATE_MATERIAL_MASTER"))   		//Create Matrial Master 
        {
        	try {
        		
        		new MmMatmasInbService().CreatedSync(input, tables, output, "ZTMM_OUT_MATMASTER");
        		
			} catch (Exception e) {
				log.error(e.getMessage());
				System.err.println(e.getMessage());
			}
        }
        else if(function.getName().equals("ZCREATE_MATERIAL_CONVFACTOR")){		//Create Matrial Conversion factor
        	try {
        		new MmConvFactInbService().CreatedSync(input, tables, output, "ZTMM_OUT_MATCONVFACTOR");
        		
			} catch (Exception e) {
				log.error(e.getMessage());
				System.err.println(e.getMessage());
			}
        }
        else if(function.getName().equals("ZCREATE_PLANT_MASTER")){			//Create Project Plant
        	try {
        		
        		new MmPlantInbService().CreatedSync(input, tables, output, "ZTMM_OUT_PLANT");
				
			} catch (Exception e) {
				log.error(e.getMessage());
				System.err.println(e.getMessage());
			}
        }        
        else if(function.getName().equals("ZCREATE_VENDOR_MASTER")){		//Create Vendor Master
        	try {
        		new MmVendorMasInbService().CreatedSync(input, tables, output, "ZTFI_OUT_VENDOR");
        		
        	} catch (Exception e) {
        		log.error(e.getMessage());
    			System.err.println(e.getMessage());
        	}
        } 
        else if(function.getName().equals("ZCREATE_PO_CONSTRUCTION")){			//Create Purchase Order 
        	try {
        		new MmPurchaseOrderService().CreatedSync(input, tables, output, "ZTMM_OUT_PO");
        		
        	} catch (Exception e) {
        		log.error(e.getMessage());
    			System.err.println(e.getMessage());
        	}
        }       
        else if(function.getName().equals("ZCREATE_OUT_PAYMENT")){		//Create Out going payment
        	try {
        		new PaymentRequestOutgoingService().CreatedSync(input, tables, output, "ZTFI_OUT_PAY");
        		
        	} catch (Exception e) {
        		log.error(e.getMessage());
    			System.err.println(e.getMessage());
        	}
        }       
        else if(function.getName().equals("ZCREATE_RECEIPT_RE")){		//Create Receipted RE
        	try {
        		new ReReceiptedService().CreatedSync(input, tables, output, "ZTFI_OUT_REC_RE");
        		
        	} catch (Exception e) {
        		log.error(e.getMessage());
    			System.err.println(e.getMessage());
        	}
        } 
        else if(function.getName().equals("ZCREATE_LIV_CN")){		//Create LIV AccountPayAble
        	try {
        		new MmSuppdebitnotesinbService().CreatedSync(input, tables, output, "ZTMM_OUT_LIV");
        	} catch (Exception e) {
        		log.error(e.getMessage());
    			System.err.println(e.getMessage());
        	}
        }
    }
}
